// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.profanity.locales;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import zombie.profanity.Phonizer;
import zombie.profanity.ProfanityFilter;

public abstract class Locale {
    protected String id;
    protected int storeVowelsAmount = 3;
    protected String phoneticRules = "";
    protected Map<String, Phonizer> phonizers = new HashMap();
    protected Map<String, String> filterWords = new HashMap();
    protected List<String> filterWordsRaw = new ArrayList();
    protected List<String> filterContains = new ArrayList();
    protected ArrayList<String> whitelistWords = new ArrayList();
    protected Pattern pattern;
    private Pattern preProcessLeet = Pattern.compile("(?<leet>[\\$@34701])\\k<leet>*|(?<nonWord>[^A-Z\\s\\$@34701]+)");
    private Pattern preProcessDoubles = Pattern.compile("(?<doublechar>[A-Z])\\k<doublechar>+");
    private Pattern preProcessVowels = Pattern.compile("(?<vowel>[AOUIE])");

    protected Locale(String string) {
        this.id = string;
        this.Init();
        this.finalizeData();
        this.loadFilterWords();
        this.loadFilterContains();
        this.loadWhiteListWords();
        ProfanityFilter.printDebug("Done init locale: " + this.id);
    }

    public String getID() {
        return this.id;
    }

    public String getPhoneticRules() {
        return this.phoneticRules;
    }

    public int getFilterWordsCount() {
        return this.filterWords.size();
    }

    protected abstract void Init();

    public void addWhiteListWord(String word) {
        word = word.toUpperCase().trim();
        if (!this.whitelistWords.contains(word)) {
            this.whitelistWords.add(word);
        }
    }

    public void removeWhiteListWord(String word) {
        word = word.toUpperCase().trim();
        if (this.whitelistWords.contains(word)) {
            this.whitelistWords.remove(word);
        }
    }

    public boolean isWhiteListedWord(String str) {
        return this.whitelistWords.contains(str.toUpperCase().trim());
    }

    public void addFilterWord(String word) {
        String string0 = this.phonizeWord(word);
        if (string0.length() > 2) {
            String string1 = "";
            if (this.filterWords.containsKey(string0)) {
                string1 = string1 + (String)this.filterWords.get(string0) + ",";
            }

            ProfanityFilter.printDebug("Adding word: " + word + ", Phonized: " + string0);
            this.filterWords.put(string0, string1 + word.toLowerCase());
        } else {
            ProfanityFilter.printDebug("Refusing word: " + word + ", Phonized: " + string0 + ", null or phonized < 2 characters");
        }
    }

    public void removeFilterWord(String word) {
        String string = this.phonizeWord(word);
        if (this.filterWords.containsKey(string)) {
            this.filterWords.remove(string);
        }
    }

    public void addFilterContains(String str) {
        if (str != null && !str.isEmpty() && !this.filterContains.contains(str.toUpperCase())) {
            this.filterContains.add(str.toUpperCase());
        }
    }

    public void removeFilterContains(String str) {
        this.filterContains.remove(str.toUpperCase());
    }

    public void addFilterRawWord(String word) {
        if (word != null && !word.isEmpty() && !this.filterWordsRaw.contains(word.toUpperCase())) {
            this.filterWordsRaw.add(word.toUpperCase());
        }
    }

    public void removeFilterWordRaw(String word) {
        this.filterWordsRaw.remove(word.toUpperCase());
    }

    protected String repeatString(int _int, char char1) {
        char[] char0 = new char[_int];
        Arrays.fill(char0, char1);
        return new String(char0);
    }

    protected boolean containsIgnoreCase(String string1, String string0) {
        if (string1 != null && string0 != null) {
            int int0 = string0.length();
            if (int0 == 0) {
                return true;
            } else {
                for (int int1 = string1.length() - int0; int1 >= 0; int1--) {
                    if (string1.regionMatches(true, int1, string0, 0, int0)) {
                        return true;
                    }
                }

                return false;
            }
        } else {
            return false;
        }
    }

    public String filterWord(String str) {
        return this.filterWord(str, false);
    }

    public String filterWord(String str, boolean includeContaining) {
        if (this.isWhiteListedWord(str)) {
            return str;
        } else {
            String string0 = this.phonizeWord(str);
            if (this.filterWords.containsKey(string0)) {
                return new String(new char[str.length()]).replace('\u0000', '*');
            } else {
                if (this.filterWordsRaw.size() > 0) {
                    for (int int0 = 0; int0 < this.filterWordsRaw.size(); int0++) {
                        if (str.equalsIgnoreCase((String)this.filterWordsRaw.get(int0))) {
                            return new String(new char[str.length()]).replace('\u0000', '*');
                        }
                    }
                }

                if (includeContaining) {
                    for (int int1 = 0; int1 < this.filterContains.size(); int1++) {
                        String string1 = (String)this.filterContains.get(int1);
                        if (this.containsIgnoreCase(str, string1)) {
                            str = str.replaceAll("(?i)" + Pattern.quote(string1), this.repeatString(string1.length(), '*'));
                        }
                    }
                }

                return str;
            }
        }
    }

    public String validateWord(String str, boolean includeContaining) {
        if (this.isWhiteListedWord(str)) {
            return null;
        } else {
            String string0 = this.phonizeWord(str);
            if (this.filterWords.containsKey(string0)) {
                return str;
            } else {
                if (this.filterWordsRaw.size() > 0) {
                    for (int int0 = 0; int0 < this.filterWordsRaw.size(); int0++) {
                        if (str.equalsIgnoreCase((String)this.filterWordsRaw.get(int0))) {
                            return str;
                        }
                    }
                }

                if (includeContaining) {
                    for (int int1 = 0; int1 < this.filterContains.size(); int1++) {
                        String string1 = (String)this.filterContains.get(int1);
                        if (this.containsIgnoreCase(str, string1)) {
                            return string1.toLowerCase();
                        }
                    }
                }

                return null;
            }
        }
    }

    public String returnMatchSetForWord(String str) {
        String string = this.phonizeWord(str);
        return this.filterWords.containsKey(string) ? (String)this.filterWords.get(string) : null;
    }

    public String returnPhonizedWord(String str) {
        return this.phonizeWord(str);
    }

    protected String phonizeWord(String string) {
        string = string.toUpperCase().trim();
        if (this.whitelistWords.contains(string)) {
            return string;
        } else {
            string = this.preProcessWord(string);
            if (this.phonizers.size() <= 0) {
                return string;
            } else {
                Matcher matcher = this.pattern.matcher(string);
                StringBuffer stringBuffer = new StringBuffer();

                while (matcher.find()) {
                    for (Entry entry : this.phonizers.entrySet()) {
                        if (matcher.group((String)entry.getKey()) != null) {
                            ((Phonizer)entry.getValue()).execute(matcher, stringBuffer);
                            break;
                        }
                    }
                }

                matcher.appendTail(stringBuffer);
                return stringBuffer.toString();
            }
        }
    }

    private String preProcessWord(String string0) {
        Matcher matcher = this.preProcessLeet.matcher(string0);
        StringBuffer stringBuffer = new StringBuffer();

        while (matcher.find()) {
            if (matcher.group("leet") != null) {
                String string1 = matcher.group("leet").toString();
                switch (string1) {
                    case "$":
                        matcher.appendReplacement(stringBuffer, "S");
                        break;
                    case "4":
                    case "@":
                        matcher.appendReplacement(stringBuffer, "A");
                        break;
                    case "3":
                        matcher.appendReplacement(stringBuffer, "E");
                        break;
                    case "7":
                        matcher.appendReplacement(stringBuffer, "T");
                        break;
                    case "0":
                        matcher.appendReplacement(stringBuffer, "O");
                        break;
                    case "1":
                        matcher.appendReplacement(stringBuffer, "I");
                }
            } else if (matcher.group("nonWord") != null) {
                matcher.appendReplacement(stringBuffer, "");
            }
        }

        matcher.appendTail(stringBuffer);
        matcher = this.preProcessDoubles.matcher(stringBuffer.toString());
        stringBuffer.delete(0, stringBuffer.capacity());

        while (matcher.find()) {
            if (matcher.group("doublechar") != null) {
                matcher.appendReplacement(stringBuffer, "${doublechar}");
            }
        }

        matcher.appendTail(stringBuffer);
        matcher = this.preProcessVowels.matcher(stringBuffer.toString());
        stringBuffer.delete(0, stringBuffer.capacity());
        int _int = 0;

        while (matcher.find()) {
            if (matcher.group("vowel") != null) {
                if (_int < this.storeVowelsAmount) {
                    matcher.appendReplacement(stringBuffer, "${vowel}");
                    _int++;
                } else {
                    matcher.appendReplacement(stringBuffer, "");
                }
            }
        }

        matcher.appendTail(stringBuffer);
        return stringBuffer.toString();
    }

    protected void addPhonizer(Phonizer phonizer) {
        if (phonizer != null && !this.phonizers.containsKey(phonizer.getName())) {
            this.phonizers.put(phonizer.getName(), phonizer);
        }
    }

    protected void finalizeData() {
        this.phoneticRules = "";
        int int0 = this.phonizers.size();
        int int1 = 0;

        for (Phonizer phonizer : this.phonizers.values()) {
            this.phoneticRules = this.phoneticRules + phonizer.getRegex();
            if (++int1 < int0) {
                this.phoneticRules = this.phoneticRules + "|";
            }
        }

        ProfanityFilter.printDebug("PhoneticRules: " + this.phoneticRules);
        this.pattern = Pattern.compile(this.phoneticRules);
    }

    protected void loadFilterWords() {
        try {
            String string0 = zombie.ZomboidFileSystem.instance.getString(ProfanityFilter.LOCALES_DIR + "blacklist_" + this.id + ".txt");
            File file = new File(string0);
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String string1;
            int _int;
            for (_int = 0; (string1 = bufferedReader.readLine()) != null; _int++) {
                this.addFilterWord(string1);
            }

            fileReader.close();
            ProfanityFilter.printDebug("BlackList, " + _int + " added.");
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    protected void loadFilterContains() {
        try {
            String string0 = zombie.ZomboidFileSystem.instance.getString(ProfanityFilter.LOCALES_DIR + "blacklist_contains_" + this.id + ".txt");
            File file = new File(string0);
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            int _int = 0;

            String string1;
            while ((string1 = bufferedReader.readLine()) != null) {
                if (!string1.startsWith("//")) {
                    this.addFilterContains(string1);
                    _int++;
                }
            }

            fileReader.close();
            ProfanityFilter.printDebug("BlackList contains, " + _int + " added.");
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    protected void loadWhiteListWords() {
        try {
            String string0 = zombie.ZomboidFileSystem.instance.getString(ProfanityFilter.LOCALES_DIR + "whitelist_" + this.id + ".txt");
            File file = new File(string0);
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String string1;
            int _int;
            for (_int = 0; (string1 = bufferedReader.readLine()) != null; _int++) {
                this.addWhiteListWord(string1);
            }

            fileReader.close();
            ProfanityFilter.printDebug("WhiteList, " + _int + " added.");
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }
}
